<?php
include_once("../initial.php");
include_once("../driver/drive.php");
include_once("../driver/tableLocation.php");
include_once("../global/global_function.php");
class userRHandle
{
    public function get_all_data($orderby="", $limit="")
    {
        $tablename = tableLocation::userTable();
        $sql = "select * from $tablename $orderby $limit";
        return drive::get_all_rows($sql);
    }
    public function get_user_num($array)
    {
        $tablename = tableLocation::userTable();
        $sql = get_select_sql($tablename, "count(*)", $array);
        $result = drive::get_one_value($sql);
        return $result;
    }
    public function get_some_data($array, $orderby='', $limit='')
    {
        $tablename = tableLocation::userTable();
        $sql = "SELECT * FROM $tablename WHERE ";
        $first=0;
        foreach($array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= " `$key`='$value' ";
                $first=1;
            }
            else
            {
                $sql .= "and `$key`='$value'";
            }
        }
        $sql .= " $orderby $limit";
        //echo $sql;
        return drive::get_all_rows($sql);
    }
    
    public function get_user_course_number($conditionArray)
    {
        $tablename = tableLocation::userCourseTable();
        $sql = get_select_sql($tablename, "count(*)", $conditionArray);
        $result = drive::get_one_value($sql);
        return $result;
    }
    
		public function get_user_courses($conditionArray, $orderby='', $limit='')
		{
				$tablename = tableLocation::userCourseTable();
        $sql = "SELECT * FROM $tablename WHERE ";
        $first=0;
        foreach($conditionArray as $key=>$value)
        {
            if($first==0)
            {
                $sql .= " `$key`='$value' ";
                $first=1;
            }
            else
            {
                $sql .= "and `$key`='$value'";
            }
        }
        if($orderby=='')
        {
        		$orderby = "order by createtime desc";
        }
        $sql .= " $orderby $limit";
        return drive::get_all_rows($sql);
		}
		public function get_booktime_by_user_course($uid, $cid)
		{
				$tablename = tableLocation::userCourseTable();
				$sql = "select booktime from $tablename where uid='$uid' and cid='$cid'";
				return drive::get_all_rows($sql);
				
		}
		public function check_booktime($uid, $booktime)
		{
				$tablename = tableLocation::userTable();
				$sql = "select count(*) from $tablename where id='$uid' and booktime>=$booktime";
				$result=drive::get_all_rows($sql);
				if(count($result)>0)
				{
						return true;
				}
				return false;
		}
		public function check_booktime_in_course($uid, $cid, $booktime)
		{
				$tablename = tableLocation::userCourseTable();
				$sql = "select uid from $tablename where uid='$uid' and cid='$cid' and booktime>=$booktime";
				$result=drive::get_all_rows($sql);
				if(count($result)>0)
				{
						return true;
				}
				return false;
		}
		public function search_user_by_keyword($keyword, $limit)
		{
				$table = tableLocation::userTable();
				$sql = "select * from $table where id like '%$keyword%' or name like '%$keyword%' order by createtime desc $limit";
				$result=drive::get_all_rows($sql);
				return $result;
		}
}
?>